    package com.test.noteapp.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.test.noteapp.EditActivity;
import com.test.noteapp.NoteDbOpenHelper;
import com.test.noteapp.R;
import com.test.noteapp.bean.Note;
import com.test.noteapp.util.ToastUtil;

import java.lang.reflect.Type;
import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {


    private List<Note>mBeanList;
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private NoteDbOpenHelper mNoteDbOpenHelper;


public MyAdapter(Context context,List<Note> mBeanList){
    this.mBeanList = mBeanList;
    this.mContext = context;
    mLayoutInflater = LayoutInflater.from(mContext);
    mNoteDbOpenHelper = new NoteDbOpenHelper(mContext);
}



    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

     View view = mLayoutInflater.inflate(R.layout.list_item_layout,parent,false);

     MyViewHolder myViewHolder = new MyViewHolder(view);

        return myViewHolder;
    }


    //设置值
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    Note note = mBeanList.get(position);

    holder.mTvTitle.setText(note.getTitle());
    holder.mTvContent.setText(note.getContent());
    holder.mTvTime.setText(note.getCreatedTime());
    //跳转
        holder.rlContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(mContext, EditActivity.class);
                intent.putExtra("note", note);
                mContext.startActivity(intent);
//传note
            }
        });

        holder.rlContainer.setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                //长按弹出窗口删除or编辑
                Dialog dialog = new Dialog(mContext,android.R.style.ThemeOverlay_Material_Dialog_Alert);
                View view = mLayoutInflater.inflate(R.layout.list_item_dialog_layout,null);

                TextView tvDelete = view.findViewById(R.id.tv_delete);
                TextView tvEdit = view.findViewById(R.id.tv_edit);


                tvDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int row = mNoteDbOpenHelper.deleteFromDbById(note.getId());
                        if (row > 0) {
                            removeData(position);
                            ToastUtil.toastShort(mContext,"删除成功");
                        }else{
                            ToastUtil.toastShort(mContext,"删除失败");
                        }

                        dialog.dismiss();
                    }
                });


                tvEdit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, EditActivity.class);
                        intent.putExtra("note", note);
                        mContext.startActivity(intent);
                        dialog.dismiss();
                    }
                });


                dialog.setContentView(view);
                dialog.show();


                return false;
            }
        });
    }
    //列表数
    @Override
    public int getItemCount() {
        return mBeanList.size();
    }

    //获取数据集改变
    public void refreshData(List<Note>notes){
    this.mBeanList = notes;
    notifyDataSetChanged();
    }

    public void removeData(int pos) {
        mBeanList.remove(pos);
        notifyItemRemoved(pos);
    }



    class MyViewHolder extends RecyclerView.ViewHolder{

    TextView mTvTitle;
    TextView mTvContent;
    TextView mTvTime;
    ViewGroup rlContainer;


    //声明相关控件
        public MyViewHolder(@Nullable View itemView){
            super(itemView);
            this.mTvTitle = itemView.findViewById(R.id.tv_title);
            this.mTvContent= itemView.findViewById(R.id.tv_content);
            this.mTvTime = itemView.findViewById(R.id.tv_time);
            this.rlContainer = itemView.findViewById(R.id.rl_item_container);

        }

    }

}




